$(function(){
    var obj = {
        cityName: '',
        type: $.getQueryString('type'), // 搜索类型 1:名企 2:专场 3:推荐
        columnId: $.getQueryString('columnId'), // 对应的名企id or 专场id
        title: $.getQueryString('title'),
        searchKey: localStorage.getItem('searchKey') || '',
        searchCity: '',
        cities: ['越城','诸暨','新昌','上虞','嵊州','柯桥'],
        hot: ['普工','司机','营业员','快递',
    '保安','厨师','机修','客服',
    '电工','机修','销售','服务员',
    '统计','文员','行政','财务'],
        cityStatus: false,
        sortFlag: '',
        jobList: [],
        userTel: localStorage.getItem('myTel') || '',
        userTelIpt: '',
        yzm: '',
        selId: '',
        djs: 0,
        timer: null,
        topDistance: 0,
        swiper1: null,
        swiper2: null,
        hotWord: localStorage.getItem('hotWord') || '',
        ajaxStatus: false,
        pageNo: 0,
        pageSize: 20,
        isShowTotal: false,
        init: function () {
            var that = this;
            if(that.type == '1'){ // 名企招聘
                document.title = '绍兴招聘-名企招聘';
                $.addTiTleBar(that.title);
                that.searchKey = '';
                that.hotWord = '';
                $('.searchBar').addClass('none');
            }else if(that.type == '2'){ //专场招聘
                $('#searchBar').removeClass('none');
                document.title = '绍兴招聘-专场招聘';
                that.searchKey = '';
                that.hotWord = '';
            }else if(that.type == '3'){ // 推荐职位
                $('#searchBar').removeClass('none');
                document.title = '绍兴招聘-推荐职位';
                that.searchKey = '';
                that.hotWord = '';
            }else { //主页
                $('#searchBar').removeClass('none');
                $('.jobAreaModuleBox').removeClass('none');
            }
            $('#searchIpt').val(that.searchKey);
            if(that.searchKey) $('#searchCancel').removeClass('none');
            $.initNavBar(that.type?0:1);
            that.ajaxGetJobsList(0);
            that.addEvent();
            that.initHotList();
            that.initAdsSwiper();
            $.getPositionCity(function (cityName) {
                that.cityName = cityName;
                $('#currentCity').html(that.cityName);
                that.fillSearchList(that.cities);
            })
        },
        addEvent: function () {
            var that = this;
            window.addEventListener('scroll', function () {
                // 滚动高度
                var scrollTop = 0, bodyScrollTop = 0, documentScrollTop = 0;
                bodyScrollTop = document.body.scrollTop;
                documentScrollTop = document.documentElement.scrollTop;
                scrollTop = (bodyScrollTop - documentScrollTop > 0) ? bodyScrollTop : documentScrollTop;
                //文档的总高度
                var scrollHeight = 0, bodyScrollHeight = 0, documentScrollHeight = 0;
                bodyScrollHeight = document.body.scrollHeight;
                documentScrollHeight = document.documentElement.scrollHeight;
                scrollHeight = (bodyScrollHeight - documentScrollHeight > 0) ? bodyScrollHeight : documentScrollHeight;
                //浏览器视口的高度
                var windowHeight = 0;
                windowHeight = document.documentElement.clientHeight || document.body.clientHeight;
                if(scrollTop + windowHeight > scrollHeight - 50) {
                    that.ajaxGetJobsList();
                }
                if(scrollTop > windowHeight) {
                    $('#jumpTop').show();
                }else {
                    $('#jumpTop').hide();
                }
            });
            // 20200219
            // 滑动到顶部
            $('#jumpTop').click(function () {
                document.documentElement.scrollTop = '0px';
                document.body.scrollTop = '0px'
            });
            document.getElementById('searchIpt').addEventListener('input',function () {
                that.searchKey = $(this).val();
                if(that.searchKey.length>0){
                    localStorage.setItem('searchKey',that.searchKey);
                    $('#searchCancel').removeClass('none');
                }else{
                    localStorage.removeItem('searchKey');
                    $('#searchCancel').addClass('none');
                }
                that.ajaxGetJobsList(0);
            });
            //输入取消
            $('#searchCancel').click(function () {
                $(this).addClass('none');
                that.searchKey = '';
                $('#searchIpt').val('');
                that.hotWord = '';
                $('.word').removeClass('active');
                that.ajaxGetJobsList(0);
            });
            // 下拉列表
            $('#currentCity').click(function (event) {
                event.stopPropagation();
                if($('.jobsBox').hasClass('blur')){
                    $('.jobsBox').removeClass('blur');
                    $('.searchList').slideUp();
                }
                else{
                    $('.jobsBox').addClass('blur');
                    $('.searchList').slideDown();
                    $('body').click(function () {
                        $(this).unbind();
                        $('.jobsBox').removeClass('blur');
                        $('.searchList').slideUp();
                    })
                }
            });
            $('#search').click(function () {
                that.ajaxGetJobsList(0);
            });
            // 信息登记
            $('#msgRegister').click(function () {
                that.bodyPosition();
                $('#registerPop').removeClass('none');
            })
            // 信息登记 取消按钮
            $('#registerPop .cancel').click(function () {
                that.bodyAuto();
                $('#registerPop').addClass('none');
                $('input[name=userName]').val('');
                $('input[name=age]').val('');
                $('input[name=sex]:checked').val('');
                $('input[name=earn]').val('');
                $('input[name=jobType]').val('');
                $('input[name=phone]').val('');
            });
            $('select').focus(function () {
                $(this).attr("size", 5);
            });
            $('select').blur(function () {
                $(this).removeAttr("size");
            });
            $('select').change(function () {
                $(this).removeAttr("size");
                $(this).blur();
            });
            $('#registerPop .submit').click(function () {
                that.ajaxSubmitRegister();
            })
            $('#loadingMore').click(function () {
                that.ajaxGetJobsList();
            })
            $('#navModule>li').click(function () {
                var params = $(this).attr('data-url');
                var href = location.href;
                // 登录判断
                if(!$.authIsLogin()) return;
                if(params == '1'){// 名企
                    location.href = './famousCompany.html';
                }else if(params == '2'){ //专场招聘
                    location.href = './specialArea.html';
                }else if(params == '3'){
                    // 推荐职位
                    location.href = $.changeURLPar(href,'type',params);
                }else{
                    $.alert('敬请期待');
                }
            })
        },
        addFromListEvent: function () {
            var that = this;
            $('.jobsDetailBtn').click(function () {
                // 20200219
                var id = $(this).attr('data-id');
                that.selId = id;
                if(!$.authIsLogin()) return;
                location.href ='./jobDetail.html?id='+id
            })
        },
        ajaxGetJobsList: function(pageNo) {
            var that = this;
            if(that.isShowTotal) return;
            if(that.ajaxStatus) return;
            that.ajaxStatus = true;
            if(!isNaN(pageNo)) that.pageNo = pageNo;
            that.pageNo++;
            // 通用
            var url = $.getAjaxHost('def') + 'cxjy/zpinfo/getList';
            // 名企招聘接口
            if(that.type == '1') url = $.getAjaxHost('def') + 'cxjy/zpinfo/getMQList';
            // 专场招聘接口
            else if(that.type == '2') url = $.getAjaxHost('def') + 'cxjy/zpinfo/getZCList';
            // 推荐招聘接口
            else if(that.type == '3') url = $.getAjaxHost('def') + 'cxjy/zpinfo/getTJList';
            else  url = $.getAjaxHost('def') + 'cxjy/zpinfo/getList';
            $.getData({
                url: url,
                type: 'post',
                contentType: 'application/json',
                data: JSON.stringify({
                    key:encodeURI(that.searchKey),
                    qx: encodeURI(that.searchCity),
                    flag: that.sortFlag,//（JX-降序 SX-升序）
                    hot: that.hotWord,
                    pageNo: that.pageNo,
                    pageSize: that.pageSize,
                    columnId: that.columnId
                }),
                success: function (res) {
                    that.ajaxStatus = false;
                    if(res.obj.length>0){
                        that.isShowTotal = false;
                        $('#content').show();
                        $('.noData').addClass('none');
                        if(res.obj.length == that.pageSize||that.pageNo ==1){
                            that.isShowTotal = false;
                            $('#loadingMore').show();
                        } else  that.isShowTotal = true;
                        that.fillJobsList(res.obj);
                    }else{
                        that.isShowTotal = true;
                        $('#content p').hide();
                        $('#loadingMore').hide();
                        if(that.pageNo == 1){
                            $('#content').hide();
                            $('.noData').removeClass('none');
                        }
                    }
                },
                error: function (res) {
                    that.ajaxStatus = false;
                }
            })
        },
        //信息登记 接口
        ajaxSubmitRegister: function () {
            var that = this;
            var userName = $('input[name=userName]').val();
            var age = $('input[name=age]').val();
            var sex = $('input[name=sex]:checked').val();
            var hy = $('select[name=hy]').val();
            var xl = $('select[name=xl]').val();
            var earn = $('input[name=earn]').val();
            var jobType = $('input[name=jobType]').val();
            var phone = $('input[name=phone]').val();
            if(!userName){
                $.alert('请输入姓名');
                return
            }
            if(!age){
                $.alert('请输入年龄');
                return
            }
            if(!xl){
                $.alert('请选择您的学历');
                return
            }
            if(!hy){
                $.alert('请选择行业');
                return
            }
            if(!earn){
                $.alert('请输入希望酬薪');
                return
            }
            if(!jobType){
                $.alert('请输入希望工种');
                return
            }
            if(!phone){
                $.alert('请输入手机号码');
                return
            }
            var postData = {
                userName: userName,
                age: age,
                sex: sex,
                xl: xl,
                hy: hy,
                earn: earn,
                jobType: jobType,
                phone: phone
            }
            $.getData({
                url: $.getAjaxHost('def') + 'zpinfo/getSubmitRegister',
                //url: $.getAjaxHost('def') + 'cxjy/zpinfo/getList',
                type: 'post',
                contentType: 'application/json',
                data: postData,
                success: function (res) {
                    //成功后关闭弹窗
                    $('#registerPop .cancel').click();
                    $.alert('信息登记成功，请耐心等待');
                },
                error: function (res) {
                }
            })
        },
        fillJobsList: function (list) {
            var that = this;
            var data = {};
            data.list = [];
            list.forEach(function (item) {
                item.flTags = item.fl?item.fl.split('、'):[];
                data.list.push(item);
            })
            var html = template('list', data);
            // 20200219
            if(that.pageNo == 1){
                document.documentElement.scrollTop = '0px';
                $('#content').html(html);
            }else{
                $('#content').append(html);
            }
            that.addFromListEvent();
        },
        fillSearchList: function (list) {
            var that = this;
            var data = {};
            data.localCity = that.cityName;
            data.list = list;
            var html = template('drop', data);
            $('#searchList').html(html);
            if(data.localCity){
                that.searchCity = data.localCity;
                $('.citySite.location').addClass('active');
            }else{
                $('.citySite.total').addClass('active');
            }
            that.ajaxGetJobsList(0);
            // 选择区域
            $('.citySite').click(function () {
                that.searchCity = $(this).attr('data-name');
                $('.citySite').removeClass('active');
                $(this).addClass('active');
                $('#currentCity').html(that.searchCity);
                that.ajaxGetJobsList(0);
            });
        },
        // 保持背景不滚动
        bodyPosition:function () {
            var scrollHeight = document.documentElement.scrollHeight || document.body.scrollHeight
            var scrollTop = document.documentElement.scrollTop || document.body.scrollTop
            this.topDistance = scrollTop;
            var top = $('.jobsBox').offset().top - scrollTop
            $('.jobsBox').addClass('fixed');
            $('.jobsBox').css({'top':top+'px'})
        },
        bodyAuto:function () {
            $('.jobsBox').removeClass('fixed');
            $('.jobsBox').css({'top':'0px'});
            document.documentElement.scrollTop = this.topDistance + 'px';
            document.body.scrollTop = this.topDistance + 'px'
        },
        initAdsSwiper: function () {
            var that = this;
            that.swiper2 = new Swiper('#adsBox', {
                //direction: 'vertical',
                autoplay: 5000,
                speed: 350,
                noSwiping:true,
                noSwipingClass:'swiper-slide',
                loop:true,
                autoplayDisableOnInteraction: false,
                pagination: '.swiper-pagination1',

            });
            that.swiper2 = new Swiper('#adsBoxBottom', {
                //direction: 'vertical',
                autoplay: 5000,
                speed: 350,
                noSwiping:true,
                noSwipingClass:'swiper-slide',
                loop:true,
                autoplayDisableOnInteraction: false,
                pagination: '.swiper-pagination2',

            });
        },
        initHotList: function () {
            var that = this;
            var data = {};
            data.hotList = that.hot;
            var html = template('hot', data);
            $('#hotList').html(html);
            // 20200214
            if(that.hotWord){
                that.hot.forEach(function (p1, p2, p3) {
                    if(p1 === that.hotWord){
                        $('#hotList .word').eq(p2).addClass('active');
                    }
                })
            }
            $('#hotList .word').click(function () {
                var word = $(this).attr('data-word');
                $('.word').removeClass('active');
                // 20200219
                if(that.hotWord!=word){
                    $(this).addClass('active');
                    that.hotWord = word;
                    $('#searchIpt').val(that.hotWord);
                    $('#searchCancel').removeClass('none');
                }else{
                    that.hotWord = '';
                    $('#searchIpt').val(that.hotWord);
                    $('#searchCancel').addClass('none');
                }
                //20200214
                localStorage.setItem('hotWord',that.hotWord);
                that.ajaxGetJobsList(0);
            });
            // 热门展开收起
            $('#hotClose').click(function () {
                if($(this).hasClass('active')){
                    $(this).removeClass('active');
                }else{
                    $(this).addClass('active');
                }
                $(".hotListPanel").slideToggle();
            })
        }
    };
    obj.init();
});